package yanjun.tool.validate.config;

import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import yanjun.tool.validate.bean.ValidateBeanPostProcessor;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * 验证自动配置
 *
 * @author rainn
 * @date 2019-04-24
 */
@Configuration
public class ValidationAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(Validator.class)
    public Validator validator()
    {
        ValidatorFactory factory = Validation.byProvider(HibernateValidator.class)
                .configure()
                .buildValidatorFactory();
        Validator validator = factory.getValidator();
        return validator;
    }

    @Autowired
    @Bean
    public ValidateBeanPostProcessor validateBeanPostProcessor(Validator validator)
    {
        return new ValidateBeanPostProcessor(validator);
    }
}
